import { defineStore } from 'pinia';
import sheep from '@/sheep';
import { android_code, h5_code, ios_code, mp_code, nc_prefix, nc_url, sq_prefix } from '@/sheep/config';

const AI = defineStore({
  id: 'AI',
  state: () => ({
    // 所有的会话列表
    all_session_list: [],
    // 所有的AI列表
    all_ai_list: [],
    // 上传到服务的图片路径
    nc_image: nc_url,
    // 图片路径前缀
    nc_image_path: `${nc_url}${nc_prefix}`,
    // 拾亲使用的图片路径
    sq_image_path: `${nc_url}${sq_prefix}`,
    // 所有关注列表
    all_follow_list: [],
    // 智能体的总数
    all_ai_total: 0,
    // 绑定的id
    bind_id: '',
    // 绑定的家庭ID
    bind_family_id: '',
    familyFriends: [],
    friends: {
      list: [],
    },
    genealogyFriends: [],
    version: '',
    mp_code: mp_code,
    versionInformation: {},
    shareImage: {},
  }),
  actions: {
    // 设置默认关注列表
    async setDefaultAllFollowList() {
      const { data } = await sheep.$api.chat.getDefaultFollowings();
      this.all_follow_list = data.followings;
    },
    // 设置用户家庭，家谱，好友
    async setUserFamily() {
      const { data } = await sheep.$api.user.getFriendList();
      this.friends = data.friends;
      this.familyFriends = data.familyFriends;
      this.genealogyFriends = data.genealogyFriends;
    },
    // 设置默认会话列表
    async setDefaultAllSessionList() {
      const { data } = await sheep.$api.chat.defaultSessionList();
      this.all_session_list = data.list;
    },
    // 设置所有的会话列表
    async setAllSessionList(searchContent = '') {
      const { data } = await sheep.$api.chat.nc_chat_session({
        searchContent,
      });
      this.all_session_list = data.list;
    },
    // 设置所有的AI列表
    async setAllAIList(array) {
      const { data } = await sheep.$api.AI.getLabelUsers();
      this.all_ai_list = data.labels;
      this.all_ai_total = data.total;
    },
    // 清除所有的会话列表
    async resetSessionList() {
      const follow = await sheep.$api.chat.getDefaultFollowings();
      this.all_follow_list = follow.data.followings;
      const session = await sheep.$api.chat.defaultSessionList();
      this.all_session_list = session.data.list;
    },
    // 设置所有的关注列表
    async setAllFollowList() {
      const { data } = await sheep.$api.chat.nc_chat_follow_list();
      this.all_follow_list = data.followings;
    },
    // 设置绑定的id
    setBindId(id) {
      this.bind_id = id;
    },
    // 设置绑定家庭ID
    setBindFamilyId(id) {
      this.bind_family_id = id;
    },
    setVersion(id) {
      this.version = id;
    },
    async updateEdition() {
      let type = 1;
      // #ifdef MP-WEIXIN
      this.mp_code = mp_code;
      // #endif
      // #ifdef APP-PLUS
      if (sheep.$platform.device.platform.toLocaleLowerCase() == 'ios') {
        type = 2;
        this.mp_code = ios_code;
      }
      if (sheep.$platform.device.platform.toLocaleLowerCase() == 'android') {
        type = 3;
        this.mp_code = android_code;
      }
      // #endif
      // #ifdef H5
      this.mp_code = h5_code;
      // #endif
      console.log(typeof type, type, typeof this.mp_code, this.mp_code);
      const { data } = await sheep.$api.app.appVersion({
        type,
        version: this.mp_code,
      });
      console.log('当前请求的版本号=>', this.mp_code, data);
      this.versionInformation = data;
    },
    setShareImage(url, width, height) {
      this.shareImage = {
        url,
        width,
        height,
      };
    },
  },
});

export default AI;